local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseComponent());
local CallBackManager = ClientOHOH.load.CallBackManager();
local UIManager = ClientOHOH.load.UIManager();
local CommonOHOH = _G.CommonOHOH;
local HomeGui=UIManager.ui_list.HomeGui
local LanguageHelper= _G.LanguageHelper
local GlobalTipGui = ClientOHOH.load.UIManager().ui_list.GlobalTipGui;
local BigNumberHelper = ClientOHOH.load.BigNumberHelper();
local ConnectClass=CommonOHOH.load.Connect();
local SocialService = game:GetService("SocialService")

function canSendGameInvite(sendingPlayer)
	local success, canSend = pcall(function()
	  return SocialService:CanSendGameInviteAsync(sendingPlayer)
	end)
	return success and canSend
  end

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);


	self:Show()
	HomeGui:SetAutoAttack(false)
	HomeGui:SetAutoTrain(false)	
	
	self.dataBase=self.parent:GetComponent(ClientOHOH.load.Client_PlayerDataComponent());

	self.parent.AutoTrainEvent:Connect(function(isAuto)
		HomeGui:SetAutoTrain(isAuto)
		
	end)

	self.parent.AutoAttckEvent:Connect(function(isAuto)
		HomeGui:SetAutoAttack(isAuto)
	end)

	self.parent.UserDataEvent:Connect(function(data)
		if data["base"] then
			self:UpdBaseData()
		end
	end)


	self:UpdBaseData()

	self:InitGoShow()

    self.parent:AddPrivateComponentByFolder(script.com)

end;

function M.InitGoShow(self)

	--设置
	CallBackManager:AddRbxConnect(self,HomeGui.insts.setting.button.Activated, function()
		UIManager.ui_list.SettingGui:Show()
	end);

	--宠物
	CallBackManager:AddRbxConnect(self,HomeGui.insts.pet.button.Activated, function()
		UIManager.ui_list.PetGui:Show()
		UIManager.ui_list.HomeGui:SetPetRed()
	end);

	--VIP显示
	CallBackManager:AddRbxConnect(self,HomeGui.insts.VipBuy.button.Activated, function()
			UIManager.ui_list.VipInfoGui:Show()
	end);

	--武器
	CallBackManager:AddRbxConnect(self,HomeGui.insts.weapon.button.Activated, function()
		UIManager.ui_list.WeaponStoreGui:Show()
		UIManager.ui_list.HomeGui:SetWeaponRed()
	end);

		
	-- 重生
	CallBackManager:AddRbxConnect(self,HomeGui.insts.Rebirth.button.Activated, function()
        -- UIManager.ui_list.BuffGuiRebirthGui:Show()
		UIManager.ui_list.RebirthGuiNew:Show()
	end);

	-- 好友显示
	CallBackManager:AddRbxConnect(self,HomeGui.insts.FriendEquip.button.Activated, function()
		UIManager.ui_list.HomeGui:SetFriendEquipRed()
		UIManager.ui_list.BroGui:Show()
	end);

	
	-- CallBackManager:AddRbxConnect(self,HomeGui.insts.base_power.button.Activated, function()
	-- 	--跳转到购买力量
	-- 	UIManager.ui_list.ShopGui:Go("power")
	-- end);
	CallBackManager:AddRbxConnect(self,HomeGui.insts.base_wins.button.Activated, function()
		--跳转到购买wins
		UIManager.ui_list.ShopGui:Go("wins")
	end);
	CallBackManager:AddRbxConnect(self,HomeGui.insts.base_diamond.button.Activated, function()
		--跳转到购买钻石
		UIManager.ui_list.ShopGui:Go("diamond")
	end);

	
	CallBackManager:AddRbxConnect(self,HomeGui.insts.property.button.Activated, function()
		--跳转到加点数
		UIManager.ui_list.PointGui:Show()
	end);


	--自动训练
	CallBackManager:AddRbxConnect(self,HomeGui.insts.auto_train.button.Activated, function()
		if not self.parent.character then
			return
		end
		
		assert(self.parent.character,"self.parent.character==NULL")
		self.parent:ChangeAutoTrainState()
	end);
	
	--自动攻击
	CallBackManager:AddRbxConnect(self,HomeGui.insts.auto_attack.button.Activated, function()
		if not self.parent.character then
			return
		end
		assert(self.parent.character,"self.parent.character==NULL")
		self.parent:ChangeAutoAttackState()
	end);

	-- 邀请好友
	CallBackManager:AddRbxConnect(self,HomeGui.insts.Friend.button.Activated, function()
		self:InviteFriend()
    end);

end

local CodeAnimationTool=_G.CommonOHOH.load.CodeAnimationTool();

--效果，从世界坐标，飘到UI上
function M.AddBaseItem(self,itemId,WorldPosList,count)
	local uiId=itemId
	local effectId="Effect.fly_"..itemId
	local uipos,uisize= HomeGui:GetItemAbsoluteProperties(uiId)

	for _,pos in pairs(WorldPosList) do
		CodeAnimationTool.UiAnimation.world2Screen(function()
			return _G.POOL:GetObj(effectId)
		end,function(e)
			_G.POOL:PushObj(effectId,e)
			HomeGui:AddItem(uiId,count)
		end,pos,uipos,uisize)
	end

end


function M.InviteFriend(self)

	local Players = game:GetService("Players")
	local canInvite = canSendGameInvite(Players.LocalPlayer)
	if canInvite then
	  local success, errorMessage = pcall(function()
		SocialService:PromptGameInvite(Players.LocalPlayer)
	  end)
	else
		GlobalTipGui:Show(LanguageHelper:GetText(30004))
	end
end
--添加好友加成
function M.AddFriendBoost(self,boost)
	HomeGui:AddFriendBoost(boost)
end


function M.UpdateR9Pack(self,r9Pack)
	if not r9Pack then 
		return 
	end
	local t=r9Pack
	local DAY_TIME=24*60*60
	local day1=math.floor(t/DAY_TIME)
	local day=math.floor(os.time()/DAY_TIME)
	if day1==day then

		HomeGui:HideR9Pack()
		UIManager.ui_list.R9PackGui:Hide()

	end

end

function M.UpdateVip(self,isVip,isShow)
	if isVip then
		HomeGui:HideVip()
		UIManager.ui_list.VipInfoGui:Hide()
		if isShow then
			GlobalTipGui:Show(LanguageHelper:GetText(30013))
		end
	
	end
end

function M.UpdBaseData(self)
	-- local p=math.floor(self.dataBase:GetData("base").power+0.5)
	local w=self.dataBase:GetData("base").wins
	local d=self.dataBase:GetData("base").diamond
	local m=self.dataBase:GetData("base").meat

	HomeGui:UpdBaseData(w,d,m)

end

function M.GetTopData(self)
	return self.dataBase:GetData("base")
end


function M.Show(self)

	HomeGui:Show()
end

function M.Hide(self)
	HomeGui:Hide()
end

function M.dtor(self)
	M.super.dtor(self);
end;


return M